package com.yungui.libary.handler;

import com.alibaba.fastjson.JSONObject;
import com.yungui.libary.exception.AuthenticationException;
import com.yungui.libary.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        JSONObject jsonObject = new JSONObject();
        String msg = e.getMessage();
        if (msg == null || msg.equals("")) {
            msg = "服务器出错";
        }
        jsonObject.put("message", msg);
        if (e instanceof AuthenticationException){
            jsonObject.put("code","100");
        }else if (e instanceof BusinessException){
            jsonObject.put("code","001");
        }else {
            jsonObject.put("code","000");
        }
        return jsonObject;
    }
}